import { ModalForm, ProFormText, ProFormTextArea } from '@ant-design/pro-components';
import { useModel } from '@umijs/max';
import lodash from 'lodash';
import React from 'react';

export type FormValueType = {
  target?: string;
  template?: string;
  type?: string;
  time?: string;
  frequency?: string;
} & Partial<API.RuleListItem>;

export type UpdateFormProps = {
  onChange: (bool: boolean) => void;
  onCancel: (flag?: boolean, formVals?: FormValueType) => void;
  onSubmit: (values: FormValueType) => Promise<void>;
  updateModalOpen: boolean;
  values: Partial<API.RuleListItem>;
  selectedTask?: any;
  currentForm: any;
  type: any;
};

const UpdateForm: React.FC<UpdateFormProps> = (props: any) => {
  const { initialState } = useModel('@@initialState');
  return (
    <ModalForm
      modalProps={{
        destroyOnClose: true,
      }}
      width="400px"
      labelCol={{ span: 6 }}
      wrapperCol={{ span: 14 }}
      layout="horizontal"
      open={props.updateModalOpen}
      onOpenChange={(val: boolean) => {
        props.onChange(val);
      }}
      onFinish={async (value) => {
        props.onSubmit(value);
      }}
    >
      <ProFormText
        label="集合名称"
        initialValue={props.values.nickname}
        rules={[
          {
            required: true,
            message: '集合名称不能为空',
          },
        ]}
        width="md"
        name="name"
      />
      <ProFormTextArea
        fieldProps={{
          autoSize: {
            maxRows: 16,
          },
        }}
        disabled
        initialValue={(function () {
          const res: any = [];
          const arr = props.selectedTask;
          arr.forEach((item: any) => {
            if (item.tags) {
              res.push(...item.tags.split(','));
            }
          });
          const result = lodash.uniq(res);
          return result;
        })()}
        width="md"
        name="tagList"
        label="涵盖标签"
      />
      <ProFormText
        disabled
        label="生成人员"
        initialValue={initialState?.currentUser?.name}
        width="md"
        name="createUserId"
      />
      <ProFormText
        disabled
        label="总数据量"
        initialValue={(function () {
          let res = 0;
          props.selectedTask.forEach((item: any) => {
            res += item.count;
          });
          return res;
        })()}
        width="md"
        name="count"
      />
      <ProFormTextArea
        initialValue={props.values.remark}
        width="md"
        name="remark"
        label="用途说明"
      />
    </ModalForm>
  );
};

export default UpdateForm;
